<?php

function getTimeDifference($oldDate, $newDate=null) {
    if ($oldDate == '0000-00-00 00:00:00') {
        return '';
    }

    if ($newDate == null) {
        $nowDate = time();
    }

    $newDate = strtotime($newDate);
    $oldDate = strtotime($oldDate);


    $dateDiff = $newDate - $oldDate;
//echo $newDate;
//echo $dateDiff;

    $fullYears = floor($dateDiff / (60 * 60 * 24 * 365));
    $fullMonths = floor($dateDiff / (60 * 60 * 24 * 30));
    $fullDays = floor($dateDiff / (60 * 60 * 24));
    $fullHours = floor(($dateDiff - ($fullDays * 60 * 60 * 24)) / (60 * 60)); //WRONG
    $fullMinutes = floor(($dateDiff - ($fullDays * 60 * 60 * 24) - ($fullHours * 60 * 60)) / 60); //wrong
//echo "Differernce is $fullDays days, $fullHours hours and $fullMinutes minutes. ";

    if ($fullYears > 0) {
        return $fullYears . " years";
    } else if ($fullMonths > 0) {
        return $fullMonths . " months";
    } else if ($fullDays > 0) {
        return $fullDays . " days";
    } else if ($fullHours > 0) {
        //return $fullHours . " hours";
        return $dateDiff;
    } if ($fullMinutes > 0) {
        //return $fullMinutes . " minutes";
        return $dateDiff;
    }
}

function getTimeDifferenceDays($oldDate) {
    $oldDate = strtotime($oldDate);
    $nowDate = time();
    $dateDiff = $nowDate - $oldDate;


    $fullDays = floor($dateDiff / (60 * 60 * 24));

    return $fullDays;
}

function dateDifference($startDate, $endDate=null) {
    if ($endDate == null) {
        $endDate = time();
    }


    $startDate = strtotime($startDate);
    $endDate = strtotime($endDate);
    if ($startDate === false || $startDate < 0 || $endDate === false || $endDate < 0 || $startDate > $endDate)
        return false;

    $years = date('Y', $endDate) - date('Y', $startDate);

    $endMonth = date('m', $endDate);
    $startMonth = date('m', $startDate);

    // Calculate months
    $months = $endMonth - $startMonth;
    if ($months <= 0) {
        $months += 12;
        $years--;
    }
    if ($years < 0)
        return false;

    // Calculate the days
    $offsets = array();
    if ($years > 0)
        $offsets[] = $years . (($years == 1) ? ' year' : ' years');
    if ($months > 0)
        $offsets[] = $months . (($months == 1) ? ' month' : ' months');
    $offsets = count($offsets) > 0 ? '+' . implode(' ', $offsets) : 'now';

    $days = $endDate - strtotime($offsets, $startDate);
    $days = date('z', $days);

    echo $years . " Years " . $months . " Months " . $days . " days.";

    //return array($years, $months, $days);
}

//
//function date_diff($start, $end="NOW")
//{
//        $sdate = strtotime($start);
//        $edate = strtotime($end);
//
//        $time = $edate - $sdate;
//        if($time>=0 && $time<=59) {
//                // Seconds
//                $timeshift = $time.' seconds ';
//
//        } elseif($time>=60 && $time<=3599) {
//                // Minutes + Seconds
//                $pmin = ($edate - $sdate) / 60;
//                $premin = explode('.', $pmin);
//
//                $presec = $pmin-$premin[0];
//                $sec = $presec*60;
//
//                $timeshift = $premin[0].' min '.round($sec,0).' sec ';
//
//        } elseif($time>=3600 && $time<=86399) {
//                // Hours + Minutes
//                $phour = ($edate - $sdate) / 3600;
//                $prehour = explode('.',$phour);
//
//                $premin = $phour-$prehour[0];
//                $min = explode('.',$premin*60);
//
//                $presec = '0.'.$min[1];
//                $sec = $presec*60;
//
//                $timeshift = $prehour[0].' hrs '.$min[0].' min '.round($sec,0).' sec ';
//
//        } elseif($time>=86400) {
//                // Days + Hours + Minutes
//                $pday = ($edate - $sdate) / 86400;
//                $preday = explode('.',$pday);
//
//                $phour = $pday-$preday[0];
//                $prehour = explode('.',$phour*24);
//
//                $premin = ($phour*24)-$prehour[0];
//                $min = explode('.',$premin*60);
//
//                $presec = '0.'.$min[1];
//                $sec = $presec*60;
//
//                $timeshift = $preday[0].' days '.$prehour[0].' hrs '.$min[0].' min '.round($sec,0).' sec ';
//
//        }
//        return $timeshift;
//}

function getDateDiff($date1, $date2) {
//    echo $date1;
//    echo "<br/>";
//    echo $date2;

//    echo strtotime($date1);
//    echo "<br/>";
//    echo strtotime($date2);
    
    if ($date1 == '0000-00-00 00:00:00') {
        return '';
    }

    if ($date2 == null) {
        //$date2 = date("D M j G:i:s T Y");
        $date2 = date("m-d-Y");
    }

    $diff = abs(strtotime($date1) - strtotime($date2));

    $years = floor($diff / (365 * 60 * 60 * 24));
    $months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
    $days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));
    $hours = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24) / (60 * 60));
    $minuts = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24 - $hours * 60 * 60) / 60);
    $seconds = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24 - $hours * 60 * 60 - $minuts * 60));

    //echo "<br/>";
    
    //printf("%d years, %d months, %d days, %d hours, %d minuts\n, %d seconds\n", $years, $months, $days, $hours, $minuts, $seconds);
    if ($years > 0) {
        printf("%d years ago", $years);
    } else if ($months > 0) {
        printf("%d months ago", $months);
    } else if ($days > 0) {
        printf("%d days ago", $days);
    } else if ($hours > 0) {
        printf("%d hours ago", $hours);
    } else if ($minuts > 0) {
        printf("%d minutes ago", $minuts);
    }
}
?>